package com.conversationboard.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.controller.confirm.Confirm;
import com.conversationboard.security.EncryptionException;

@WebServlet(name = "ConfirmDeleteSelectedBookmarksControllerServlet", urlPatterns = "/ConfirmDeleteSelectedBookmarksControllerServlet")
public class ConfirmDeleteSelectedBookmarksControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -913864902345052735L;


	private String generateTags(String loginId, String[] bookmarkKeys) throws EncryptionException, IOException {

		StringBuilder buffer = new StringBuilder();

		for (String key : bookmarkKeys) {
			buffer.append("<input name='checkbox' value='" + key + "' type='hidden'>");
		}

		buffer.append(Confirm.getKey(loginId));

		return buffer.toString();

	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		try {
			String[] bookmarkKeys = request.getParameterValues("checkbox");
			String loginId = request.getUserPrincipal().getName();

			String tags = this.generateTags(loginId, bookmarkKeys);

			request.setAttribute("tags", tags);

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/confirmbookmarkdelete.jsp");
			dispatcher.forward(request, response);

		} catch (NumberFormatException e) {
			throw new ServletException(e);
		} catch (EncryptionException e) {
			throw new ServletException(e);
		}

	}

}
